Q:
I've set the com.apple.macos.useScreenMenuBar property to
bring my JMenuBar to the top of the screen, as illustrated
in , but now my JCheckBoxMenuItem s fire twice when selected.
A:
This is a known issue which can be worked around using JRadioButtonMenuItem s
that have been created with the JCheckBoxMenuItem checkmark,
as set in the Swing UIManager . Note that it is important to re-set
the global icon for JRadioButtonMenuItem s when you are done
creating your checkbox "impersonator" so that you
can continue to use normal JRadioButtonMenuItem s.
Listing 1 shows some code that creates three menu items. The second item (radio2 ) demonstrates the simulation of a JCheckBoxMenuItem , while the first and third items demonstrate how you can still use regular JRadioButtonMenuItem s in conjunction with the workaround. If you discover this problem with menu items other than JCheckBoxMenuItem , please file a bug report.
String RADIO_ICON_KEY = "RadioButtonMenuItem.checkIcon"String CHECK_ICON_KEY = "CheckBoxMenuItem.checkIcon"JRadioButtonMenuItem radio1, radio2, radio3;
// Normal JRadioButtonMenuItem
radio1 = new JRadioButtonMenuItem("Radio Icon");
// Store the default JRBMI icon to put back later
Object radioIcon = UIManager.get(RADIO_ICON_KEY);
// Replace JRBMI's checkIcon with that of JCheckBoxMenuItem
// radio2 should look like a JCBMI
UIManager.put(RADIO_ICON_KEY, UIManager.get(CHECK_ICON_KEY));
JRadioButtonMenuItem radio2 = new JRadioButtonMenuItem("Check Icon");
// Put original JRBMI checkIcon back.
UIManager.put(RADIO_ICON_KEY, radioIcon);
// radio3 should now have the JRBMI icon
radio3 = new JRadioButtonMenuItem("Radio Icon");
|
Listing 1. Using JRadioButtonMenuItem s to emulate checkbox functionality
|
[Jul 12 2002]
|